package com.example.pictureview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.VideoView;

import com.example.pictureview.widget.MediaControllerView;
import com.example.pictureview.widget.VideoHelper;

import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

public class VideoActivity extends Activity {
    private VideoView mVideoView;
    private VideoHelper mVideoHelper;
    private MediaControllerView mMediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        mVideoView = (VideoView) findViewById(R.id.videoview_video);
        mVideoHelper = new VideoHelper(this, mVideoView, mMediaController);
        mMediaController = (MediaControllerView) findViewById(R.id.video_media_controller);
        mMediaController.setOnFullListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVideoHelper.rotateScreen();
            }
        });
        mVideoView.setMediaController(mMediaController);
        final Uri uri = getIntent().getData();
        mVideoView.setVideoURI(uri);
        mVideoView.start();
    }

    private void full(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attr);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    public static void gotoVideoActivity(Context context, final String url) {
        Intent intent = new Intent(context, VideoActivity.class);
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        mVideoHelper.onResume();
        mMediaController.show();
        super.onResume();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == SCREEN_ORIENTATION_PORTRAIT) {
            full(false);
        } else {
            full(true);
        }
        mVideoHelper.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoHelper.onPause();
        mMediaController.hide();
    }

    @Override
    public void onBackPressed() {
        if (mVideoHelper.onBackPressed()) {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoHelper.onDestory();
    }

}
